package com.gxa.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.utils.UploadController;
import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.R;
import com.gxa.modules.mapper.CustomerMapper;
import com.gxa.modules.pojo.Customer;
import com.gxa.modules.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 分页查询
     * @param pageSize
     * @param currentPage
     * @return
     */
    @Override
    public JsonResult findCustomerList(Long pageSize, Long currentPage) {
            JsonResult jr=new JsonResult();
            try {
                Long start =(currentPage-1)*pageSize;
                Long off = pageSize;
                Page<Customer> page =new Page(start,off);
                Page<Customer> customerPage= customerMapper.selectPage(page,null);
                jr.setCount((int) customerPage.getTotal());
                jr.setData(customerPage.getRecords());
                jr.setCode(200);
                jr.setMsg("查询成功");
            }catch (Exception e){
                e.printStackTrace();
                jr.setCode(500);
                jr.setMsg("网络炸了");
            }
            return jr;
    }

    /**
     * 条件搜索
     * @param currentPage
     * @param pageSize
     * @param customer_name
     * @param phone
     * @param creation_time
     * @return
     */
    @Override
    public JsonResult findCustomerListByCondition(Long currentPage, Long pageSize, String customer_name, String phone, String creation_time) {
        JsonResult jr=new JsonResult();
        try {
            Long start =(currentPage-1)*pageSize;
            Long off = pageSize;
            Page<Customer> page =new Page(start,off);
            QueryWrapper<Customer> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq((StringUtils.isNotBlank(customer_name)),"customer_name",customer_name)
                        .eq((StringUtils.isNotBlank(phone)),"phone",phone)
                        .eq((StringUtils.isNotBlank(creation_time)),"creation_time",creation_time);
            Page<Customer> customerPage= customerMapper.selectPage(page,queryWrapper);
            jr.setCount((int) customerPage.getTotal());
            jr.setData(customerPage.getRecords());
            jr.setCode(200);
            jr.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("网络炸了");
        }
        return jr;
    }

    @Override
    public R delCustomerById(Integer id) {
        R r = new R();
        try {
            int result =customerMapper.deleteById(id);
            if (result>0){
                r.setCode(200);
                r.setMsg("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络炸了");
        }
        return r;
    }

    @Override
    public R addCustomer(Customer customer) {
        R r =new R();
        try{
            Date date = new Date();
            SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datetime = format.format(date);
            customer.setCreationTime(datetime);
            int result = customerMapper.insert(customer);
            if (result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络卡了");
        }

        return r;
    }

    @Override
    public R updateCustomer(Customer customer) {
        R r =new R();
        try{
            int result = customerMapper.updateById(customer);
            if (result>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络卡了");
        }
        return r;
    }

    @Override
    public R updateCustomerByAuditStatus(Customer customer) {
        R r =new R();
        try{
            Date date = new Date();
            SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datetime = format.format(date);
            customer.setAuditTime(datetime);
            int result = customerMapper.updateById(customer);
            if (result>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络卡了");
        }
        return r;
    }

    @Override
    public R findCustomerById(Integer id) {
        R r = new R();
        try{
            Customer customer = customerMapper.selectById(id);
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(customer);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络炸了");
        }
        return r;
    }

    @Override
    public R uploadFile(MultipartFile file) {
        R r = new R();
        try{
            UploadController uploadController = new UploadController();
           HashMap upload= (HashMap) uploadController.upload(file);
            r.setCode(200);
            r.setMsg("上传成功");
            r.setData(upload.get("url").toString());
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("网络卡了");
        }
        return r;
    }
}
